﻿using System;
using System.Collections.Generic;
using FederatedScaleOutDatabases.DomainModel;
using FederatedScaleOutDatabases.Repositories;
using FederatedScaleOutDatabases.Strategies;

namespace FederatedDatabaseFirstAttemptConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            var federatedDatabases = new List<FederatedDatabase>()
            {
                new FederatedDatabase()
                {
                    DatabaseName="FedDb1",
                    ConnectionString=@"Data Source=.\SqlExpress;Initial Catalog=FederatedDb1;Integrated Security=True;Pooling=False"
                },
                new FederatedDatabase()
                {
                    DatabaseName="FedDb2",
                    ConnectionString=@"Data Source=.\SqlExpress;Initial Catalog=FederatedDb2;Integrated Security=True;Pooling=False"
                }
            };
            var federationStrategy = new FederationStrategy<Employee>
                                         {
                                             FederationResolutionStrategy =
                                                 new EmployeeFederationResolutionStrategy<Employee>(federatedDatabases)
                                         };


#if false
            using (var repository = new FederatedRepository<Employee>(federationStrategy, federatedDatabases))
            {
                foreach (var emp in GetSampleEmployees())
                {
                    repository.Add(emp);
                }
                repository.SaveChanges();
            }
#endif


            using (var repository = new FederatedRepository<Employee>(federationStrategy, federatedDatabases))
            {
                var results = repository.Get(e => e.City == "Bangalore" || e.City == "Delhi");

                foreach (var employee in results)
                {
                    Console.WriteLine(employee.Name);
                }

                Console.ReadLine();
                Console.Clear();

                results = repository.Get(e => e.DeptId > 100);

                foreach (var employee in results)
                {
                    Console.WriteLine(employee.Name);
                }
                Console.ReadLine();
                Console.Clear();

                results = repository.Get(e => e.Designation == ".NET Architect" && e.DateOfBirth.Year > 1980);

                foreach (var employee in results)
                {
                    Console.WriteLine(employee.Name);
                }

            }

            Console.ReadLine();
            Console.Clear();


        }       


        private static IEnumerable<Employee> GetSampleEmployees()
        {
            var random = new Random();
            for (int i = 0; i < 100; i++)
            {
                var employee1 = new Employee()
                {
                    Name = "Rahul " + i,
                    City = i % 2 == 0 ? "Bangalore" : "Delhi",
                    DateOfBirth = new DateTime(random.Next(1975, 1985), random.Next(1, 12), random.Next(1, 25)),
                    Designation = i % 2 == 0 ? ".NET Architect" : "Solution Architect"
                };

                if (employee1.City == "Bangalore")
                    employee1.DeptId = random.Next(101, 200);
                else
                    employee1.DeptId = random.Next(1, 100);

                yield return employee1;
            }
        }
    }
}
